package com.growthserver.controller;

import com.github.pagehelper.PageInfo;
import com.growthserver.enums.ResultBeans;
import com.growthserver.entity.TTaskComboEntity;
import com.growthserver.entity.TTaskInfoEntity;
import com.growthserver.entity.TTaskRuleEntity;
import com.growthserver.param.TTaskComboParam;
import com.growthserver.param.TaskInfoParam;
import com.growthserver.param.TaskInfoUserParam;
import com.growthserver.param.TaskNumUserParam;
import com.growthserver.service.TaskCenterService;
import com.growthserver.vo.TTaskComboVo;
import com.growthserver.vo.TaskInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/mp/portal/growth/taskCenter", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
@Api(description = "任务中心")
public class TaskCenterController {

    @Autowired
    private TaskCenterService taskCenterService;

    /**
     * 创建任务
     */
    @ApiOperation(value = "创建任务",notes = "列表展示,根据type区分展示内容")
    @PostMapping("/createTask")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.taskCenter.createTask", type = ApiType.PORTAL)
//    }, remark = "任务中心--创建任务", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> createTask(@RequestBody TaskInfoVo taskInfoVo,
                                          @RequestHeader(required = false, name = "uid") String uid,
                                          @RequestHeader(required = false, name = "userName") String userName) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            //规则校验
            boolean aBoolean = taskCenterService.checkRule(taskInfoVo);
            if(aBoolean){
                taskInfoVo.setCreator(uid);
                taskInfoVo.setCreatedName(userName);
                taskCenterService.createTaskCenter(taskInfoVo);
                result.setResult("1");
                result.setMsg("任务规则创建成功");
                result.setRows(new ArrayList<>());
            }else {
                result.setResult("0");
                result.setMsg("无法创建--预审单号不在--或者存在此类型规则--不能创建多条");
                result.setRows(new ArrayList<>());
            }
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("任务规则创建异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 任务中心列表展示
     */
    @ApiOperation(value = "任务中心列表展示",notes = "列表展示,根据type区分展示内容")
    @PostMapping("/taskDisplay")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.taskCenter.taskDisplay", type = ApiType.PORTAL)
//    }, remark = "任务中心--任务中心列表展示", qps = 100, connectTimeout = 5)
    public ResultBeans<List<TTaskInfoEntity>> taskDisplay(@RequestBody TaskInfoParam taskInfoParam) {
        ResultBeans<List<TTaskInfoEntity>> result = new ResultBeans<>();
        try {
            List<TTaskInfoEntity> tIovTaskInfoList = taskCenterService.taskDisplay(taskInfoParam);
            PageInfo<TTaskInfoEntity> tIovTaskInfoPageInfo = new PageInfo<>(tIovTaskInfoList);
            result.setResult("1");
            result.setMsg("任务中心列表展示成功");
            result.setRows(tIovTaskInfoList);
            result.setTotal((int)tIovTaskInfoPageInfo.getTotal());
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("任务中心列表展示异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 修改任务
     */
    @ApiOperation(value = "修改任务",notes = "列表展示,根据type区分展示内容")
    @PostMapping("/updateTask")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.taskCenter.updateTask", type = ApiType.PORTAL)
//    }, remark = "任务中心--修改任务", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> updateTask(@RequestBody TTaskInfoEntity tTaskInfoEntity,
                                          @RequestHeader(required = false, name = "uid") String uid,
                                          @RequestHeader(required = false, name = "userName") String userName) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            tTaskInfoEntity.setModifier(uid);
            tTaskInfoEntity.setModifyName(userName);
            taskCenterService.updateTask(tTaskInfoEntity);
            result.setResult("1");
            result.setMsg("修改任务成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("修改任务异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 上下架
     */
    @ApiOperation(value = "上下架",notes = "列表展示,根据type区分展示内容")
    @PostMapping("/upDownTask")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.taskCenter.upDownTask", type = ApiType.PORTAL)
//    }, remark = "任务中心--上下架", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> upDownTask(@RequestBody TaskInfoParam taskInfoParam) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            taskCenterService.upDownTask(taskInfoParam.getIsEnable(),taskInfoParam.getTaskId());
            result.setResult("1");
            result.setMsg("上下架成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("上下架异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }


    /**
     * 单条任务查看详情
     */
    @ApiOperation(value = "单条任务查看详情",notes = "列表展示,根据type区分展示内容")
    @PostMapping("/selectTaskRules")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.taskCenter.selectTaskRules", type = ApiType.PORTAL)
//    }, remark = "任务中心--单条任务查看详情", qps = 100, connectTimeout = 5)
    public ResultBeans<List<TTaskRuleEntity>> selectTaskRules(@RequestBody TaskInfoParam taskInfoParam) {
        ResultBeans<List<TTaskRuleEntity>> result = new ResultBeans<>();
        try {
            List<TTaskRuleEntity> tTaskRuleEntities = taskCenterService.selectTaskRules(taskInfoParam.getTaskId());
            result.setResult("1");
            result.setMsg("单条任务查看详情成功");
            result.setRows(tTaskRuleEntities);
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("单条任务查看详情异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 任务规则删除
     */
    @ApiOperation(value = "任务规则删除",notes = "列表展示,根据type区分展示内容")
    @PostMapping("/deleteTask")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.taskCenter.deleteTask", type = ApiType.PORTAL)
//    }, remark = "任务中心--任务规则删除", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> deleteTask(@RequestBody TTaskComboParam tTaskComboParam) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            taskCenterService.deleteTask(tTaskComboParam.getTaskId());
            result.setResult("1");
            result.setMsg("上下架社区任务数成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("上下架社区任务数异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 创建任务件数规则
     */
    @ApiOperation(value = "创建任务件数规则",notes = "列表展示,根据type区分展示内容")
    @PostMapping("/createTaskNum")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.taskCenter.createTaskNum", type = ApiType.PORTAL)
//    }, remark = "任务中心--创建任务件数规则", qps = 100, connectTimeout = 5)
    public ResultBeans<List<TTaskRuleEntity>> createTaskNum(@RequestBody TTaskComboVo tTaskComboVo,
                                                            @RequestHeader(required = false, name = "uid") String uid,
                                                            @RequestHeader(required = false, name = "userName") String userName) {
        ResultBeans<List<TTaskRuleEntity>> result = new ResultBeans<>();
        try {
            tTaskComboVo.setCreator(uid);
            tTaskComboVo.setCreatedName(userName);
            taskCenterService.createTaskNum(tTaskComboVo);
            result.setResult("1");
            result.setMsg("创建任务件数规则成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("创建任务件数规则异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 编辑任务件数规则
     */
    @ApiOperation(value = "编辑任务件数规则",notes = "列表展示,根据type区分展示内容")
    @PostMapping("/updateTaskNum")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.taskCenter.updateTaskNum", type = ApiType.PORTAL)
//    }, remark = "任务中心--编辑任务件数规则", qps = 100, connectTimeout = 5)
    public ResultBeans<List<TTaskRuleEntity>> updateTaskNum(@RequestBody TTaskComboEntity tTaskComboEntity,
                                                            @RequestHeader(required = false, name = "uid") String uid,
                                                            @RequestHeader(required = false, name = "userName") String userName) {
        ResultBeans<List<TTaskRuleEntity>> result = new ResultBeans<>();
        try {
            tTaskComboEntity.setModifier(uid);
            tTaskComboEntity.setModifyName(userName);
            taskCenterService.updateTaskNum(tTaskComboEntity);
            result.setResult("1");
            result.setMsg("编辑任务件数规则成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("编辑任务件数规则异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 上下架社区任务数
     */
    @ApiOperation(value = "上下架社区任务数",notes = "列表展示,根据type区分展示内容")
    @PostMapping("/upDownTaskNum")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.taskCenter.upDownTaskNum", type = ApiType.PORTAL)
//    }, remark = "任务中心--上下架社区任务数", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> upDownTaskNum(@RequestBody TTaskComboParam tTaskComboParam) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            taskCenterService.upDownTaskNum(tTaskComboParam.getIsEnable(),tTaskComboParam.getTaskId());
            result.setResult("1");
            result.setMsg("上下架社区任务数成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("上下架社区任务数异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }



    /**
     * 查询社区任务列表
     */
    @ApiOperation(value = "查询社区任务列表",notes = "列表展示,根据type区分展示内容")
    @PostMapping("/taskNumDisplay")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.taskCenter.taskNumDisplay", type = ApiType.PORTAL)
//    }, remark = "任务中心--查询社区任务列表", qps = 100, connectTimeout = 5)
    public ResultBeans<List<TTaskComboEntity>> taskNumDisplay(@RequestBody TTaskComboParam tTaskComboParam) {
        ResultBeans<List<TTaskComboEntity>> result = new ResultBeans<>();
        try {
            List<TTaskComboEntity> tTaskComboEntity = taskCenterService.taskNumDisplay(tTaskComboParam);
            result.setRows(tTaskComboEntity);
            result.setResult("1");
            result.setMsg("查询社区任务列表成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("查询社区任务列表异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }


    /**
     * 删除社区任务
     */
    @ApiOperation(value = "删除社区任务",notes = "列表展示,根据type区分展示内容")
    @PostMapping("/deleteTaskNum")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.taskCenter.deleteTaskNum", type = ApiType.PORTAL)
//    }, remark = "任务中心--删除社区任务", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> deleteTaskNum(@RequestBody TTaskComboParam tTaskComboParam) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            taskCenterService.deleteTaskNum(tTaskComboParam.getTaskId());
            result.setResult("1");
            result.setMsg("删除社区任务成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("删除社区任务异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 用户等级信息获取
     */
    @ApiOperation(value = "用户等级信息获取",notes = "列表展示,根据type区分展示内容")
    @PostMapping("/getUserMemberInfo")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.taskCenter.getUserMemberInfo", type = ApiType.PORTAL)
//    }, remark = "任务中心--用户等级信息获取", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> getUserMemberInfo(@RequestBody TTaskComboParam tTaskComboParam) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            List<HashMap<String, String>> userMemberInfo = taskCenterService.getUserMemberInfo(tTaskComboParam);
            result.setRows(userMemberInfo);
            result.setResult("1");
            result.setMsg("用户等级信息获取成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("用户等级信息获取异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 用户任务列表
     */
    @ApiOperation(value = "用户任务列表",notes = "用户任务列表")
    @PostMapping("/taskDisplayByUser")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.miniprogram.growth.taskCenter.taskDisplayByUser", type = ApiType.MINI_PROGRAM),
//            @GwApiDesc(name = "ly.mp.app.growth.taskCenter.taskDisplayByUser", type = ApiType.APP)
//    }, remark = "用户任务列表", qps = 100, connectTimeout = 5)
    public ResultBeans<List<TaskInfoUserParam>> taskDisplayByUser(@RequestHeader(required = false, name = "uid") String userId) {
        ResultBeans<List<TaskInfoUserParam>> result = new ResultBeans<>();
        if (ObjectUtils.isEmpty(userId)){
            result.setResult("1");
            result.setMsg("用户任务列表展示成功");
            result.setRows(new ArrayList<>());
            return result;
        }
        try {
            List<TaskInfoUserParam> tIovTaskInfoList = taskCenterService.taskDisplayByUser(userId);
            result.setResult("1");
            result.setMsg("用户任务列表展示成功");
            result.setRows(tIovTaskInfoList);
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("用户任务列表展示异常");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    @ApiOperation(value = "用户完成任务统计",notes = "用户完成任务统计")
    @PostMapping("/taskDisplayByUserNum")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.miniprogram.growth.taskCenter.taskDisplayByUserNum", type = ApiType.MINI_PROGRAM),
//            @GwApiDesc(name = "ly.mp.app.growth.taskCenter.taskDisplayByUserNum", type = ApiType.APP)
//    }, remark = "用户完成任务统计", qps = 100, connectTimeout = 5)
    public ResultBeans<TaskNumUserParam> taskDisplayByUserNum(@RequestHeader(required = false, name = "uid") String userId) {
        ResultBeans<TaskNumUserParam> result = new ResultBeans<>();
        if (ObjectUtils.isEmpty(userId)){
            result.setResult("1");
            result.setMsg("用户完成任务统计展示成功");
            result.setRows(new TaskNumUserParam());
            return result;
        }
        try {
            TaskNumUserParam taskNumUserParam = taskCenterService.taskDisplayByUserNum(userId);
            result.setResult("1");
            result.setMsg("用户完成任务统计展示成功");
            result.setRows(taskNumUserParam);
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("用户完成任务统计展示异常");
            result.setRows(new TaskNumUserParam());
        }
        return result;
    }
}
